package com.download.web.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.download.web.entity.BusUserHistory;
import com.download.web.entity.SysNavBar;
import com.download.web.entity.SysUser;
import com.download.web.entity.model.ArticleListModel;
import com.download.web.entity.model.ArticleModel;
import com.download.web.entity.model.CommentModel;
import com.download.web.entity.model.Page;
import com.download.web.service.IBusArticleCommentService;
import com.download.web.service.IBusArticleService;
import com.download.web.service.IBusUserHistoryService;
import com.download.web.service.ISysNavBarService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.xml.stream.events.Comment;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author cmy
 * @since 2019-02-20
 */
@Controller
@RequestMapping("/download/article")
public class BusArticleController{

    @Autowired
    private IBusArticleService articleService;

    @Autowired
    private ISysNavBarService navService;
    @Autowired
    private IBusArticleCommentService articleCommentService;

    @Autowired
    private IBusUserHistoryService userHistoryService;


    @ResponseBody
    @RequestMapping("/selectArticlesPage")
    public Page<ArticleListModel> selectArticles(Integer pageIndex,Integer pageSize){
        Page<ArticleListModel> articles=articleService.articlePage(pageIndex,pageSize,0);

        return articles;
    }
    /**
     * 按文章id查找文章信息
     * @param id
     */
    @RequestMapping("/findArticleById")
    public ModelAndView findArticleById(@RequestParam("id") Integer id, HttpServletRequest request){
        articleService.addBrowseNum(id);
        SysUser user=(SysUser)request.getSession().getAttribute("user");
        if(user!=null) {
            BusUserHistory uh=userHistoryService.getOne(new QueryWrapper<BusUserHistory>().eq("user_id",user.getId()).eq("article_id",id));
            if(uh==null) {
                userHistoryService.save(new BusUserHistory(user.getId(), id));
            }
        }
        ArticleModel articleModel= articleService.findArticleById(id);
        Page<CommentModel> commentModelPage=articleCommentService.commentListPage(id,0,5,0);
        ModelAndView mv = new ModelAndView();
        mv.setViewName("web/content");
        mv.addObject("article",articleModel);
        mv.addObject("comments",commentModelPage);
        return mv;
    }

    @RequestMapping("/selectArticlesByType")
    public String selectArticlesByType(@RequestParam(value = "pageIndex",defaultValue = "1") Integer pageIndex, @RequestParam(value = "pageSize",defaultValue = "30") Integer pageSize,
                                       Integer type, Model model){

        Page<ArticleListModel> articles=articleService.articlePageByType(pageIndex,pageSize,type);
        model.addAttribute("articles",articles);
        model.addAttribute("type",type);
        List<SysNavBar> str=navService.selectTypeName(type);
        if (str.size()>1){
            model.addAttribute("parNav",str.get(1));
            model.addAttribute("nav",str.get(0));
        }else{
            model.addAttribute("parNav",str.get(0));
        }
        return "web/list";
    }


    @ResponseBody
    @RequestMapping("/articlesByTypePage")
    public Page<ArticleListModel> articlesByTypePage(@RequestParam(value = "pageIndex",defaultValue = "1") Integer pageIndex, @RequestParam(value = "pageSize",defaultValue = "30") Integer pageSize, Integer type, Model model){

        Page<ArticleListModel> articles=articleService.articlePageByType(pageIndex,pageSize,type);
        model.addAttribute("articles",articles);
       /* List<SysNavBar> str=navService.selectTypeName(type);
        if (str.size()>1){
            model.addAttribute("parNav",str.get(1));
            model.addAttribute("nav",str.get(0));
        }else{
            model.addAttribute("parNav",str.get(0));
        }*/
        return articles;
    }


    @RequestMapping("/selectArticlesByKeyword")
    public String selectArticlesByKeyword(@RequestParam(value = "pageIndex",defaultValue = "0") Integer pageIndex, @RequestParam(value = "pageSize",defaultValue = "30") Integer pageSize, String keyword, Model model){
        Page<ArticleListModel> articles=articleService.articlePageByKeyword(pageIndex,pageSize,keyword);
        model.addAttribute("articles",articles);
        model.addAttribute("parNav",null);
        model.addAttribute("nav",null);
        return "web/list";
    }

    @RequestMapping("/findPageByTags")
    public ModelAndView findPageByTags(@RequestParam(value = "pageIndex",defaultValue = "0") Integer pageIndex,@RequestParam(value = "pageSize",defaultValue = "30")Integer PageSize,String tags){
        Page<ArticleListModel> articlePage=articleService.articlePageByTags(pageIndex,PageSize,1,tags);
        ModelAndView mv = new ModelAndView();
        mv.setViewName("/web/tag-article-list");
        mv.addObject("articles",articlePage);
        mv.addObject("tag",tags);
        return mv;
    }

    @ResponseBody
    @RequestMapping("/findByTagsPageList")
    public Page<ArticleListModel> findByTagsPageList(@RequestParam(value = "pageIndex",defaultValue = "0") Integer pageIndex,@RequestParam(value = "pageSize",defaultValue = "30")Integer PageSize,String tags){
        Page<ArticleListModel> articlePage=articleService.articlePageByTags(pageIndex,PageSize,1,tags);

        return articlePage;
    }

}
